package cn.trinea.android.demo;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import cn.trinea.android.common.util.ToastUtils;
import cn.trinea.android.common.view.DropDownListView;
import cn.trinea.android.common.view.DropDownListView.OnDropDownListener;

/**
 * DropDownListViewDemo
 * 
 * @author <a href="http://www.trinea.cn/android/dropdown-to-refresh-and-bottom-load-more-listview/" target="_blank">Trinea</a> 2013-6-1
 */
public class DropDownListViewDemo extends BaseActivity {

	private LinkedList<String> listItems = null;
	private DropDownListView listView = null;
	private ArrayAdapter<String> adapter;

	private String[] mStrings = { "Aaaaaa", "Bbbbbb", "Cccccc", "Dddddd", "Eeeeee", "Ffffff", "Gggggg", "Hhhhhh", "Iiiiii", "Jjjjjj", "Kkkkkk",
			"Llllll", "Mmmmmm", "Nnnnnn", };
	public static final int MORE_DATA_MAX_COUNT = 3;
	public int moreDataCount = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.drop_down_listview_demo);

		listView = (DropDownListView) findViewById(R.id.list_view);
		// set drop down listener
		listView.setOnDropDownListener(new OnDropDownListener() {

			@Override
			public void onDropDown() {
				new GetDataTask(true).execute();
			}
		});

		// set on bottom listener
		listView.setOnBottomListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new GetDataTask(false).execute();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ToastUtils.show(context, R.string.drop_down_tip);
			}
		});
		// listView.setShowFooterWhenNoMore(true);

		listItems = new LinkedList<String>();
		listItems.addAll(Arrays.asList(mStrings));
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
		listView.setAdapter(adapter);
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		private boolean isDropDown;

		public GetDataTask(boolean isDropDown) {
			this.isDropDown = isDropDown;
		}

		@Override
		protected String[] doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				;
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {

			if (isDropDown) {
				listItems.addFirst("Added after drop down");
				adapter.notifyDataSetChanged();

				// should call onDropDownComplete function of DropDownListView at end of drop down complete.
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
				listView.onDropDownComplete(getString(R.string.update_at) + dateFormat.format(new Date()));
			} else {
				moreDataCount++;
				listItems.add("Added after on bottom");
				adapter.notifyDataSetChanged();

				if (moreDataCount >= MORE_DATA_MAX_COUNT) {
					listView.setHasMore(false);
				}

				// should call onBottomComplete function of DropDownListView at end of on bottom complete.
				listView.onBottomComplete();
			}

			super.onPostExecute(result);
		}
	}
}
